package com.kid.kidliving.commodity.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kid.common.utils.PageUtils;
import com.kid.common.utils.Query;

import com.kid.kidliving.commodity.dao.AttrgroupDao;
import com.kid.kidliving.commodity.entity.AttrgroupEntity;
import com.kid.kidliving.commodity.service.AttrgroupService;


@Service("attrgroupService")
public class AttrgroupServiceImpl extends ServiceImpl<AttrgroupDao, AttrgroupEntity> implements AttrgroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                new QueryWrapper<AttrgroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        //获取到检索传入的关键字
        String key = (String) params.get("key");
        //根据实际情况，封装查询条件到QueryWrapper对象
        QueryWrapper<AttrgroupEntity> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(key)){
            //id是相等条件，name是模糊查询条件
            //wrapper.eq("id",key).or().like("name",key);
            //wrapper.and,可以将查询条件作为独立的一组
            wrapper.and((obj)->{
                obj.eq("id",key).or().like("name",key);
            });
        }
        //是否需要封装categoryId条件,categoryId=0表示查询时不加入该检索条件
        if(categoryId != 0){
            wrapper.eq("category_id",categoryId);
        }
        //使用renren框架提供的业务代码
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

}